<?php
session_start();
$currentUser = $_SESSION["user"];
if(!$currentUser){
    header("localtion:login.html");
}

function getParam($name){
    $value = isset($_GET[$name])?trim($_GET[$name]):"";
    $value = str_replace("'","''",$value);
    return $value;
}

$pageIndex = isset($_GET["pageIndex"])?trim($_GET["pageIndex"]):1;
if($pageIndex<1){
    $pageIndex = 1;
}
$pageSize = isset($_GET["pageSize"])?trim($_GET["pageSize"]):2;
if($pageSize<1){
    $pageSize = 2;
}

$where = "";
$queryTitle = getParam("Title");
if($queryTitle){
    if($where){
        $where .= " AND ";
    }
    $where .= "Title like ('%$queryTitle%')";
}

$queryAuthor = getParam("Author");
if($queryAuthor){
    if($where){
        $where .= " AND ";
    }
    $where .= "AuthorName like ('%$queryAuthor%')";
}

$queryMinDate = getParam("minDate");
if($queryMinDate){
    if($where){
        $where .= " AND ";
    }
    $where .=" CreateTime >= '$queryMinDate'";
}

$queryMaxDate = getParam("minDate");
if($queryMaxDate){
    if($where){
        $where .= " AND ";
    }
    $where .=" CreateTime <= '$queryMaxDate'";
}

if($where){
    $where =" WHERE ".$where;
}

$sqlTotal = "SELECT count(*) AS T FROM article $where";
$conn = new mysqli("localhost","root","","myblog");
$rs = $conn->query($sqlTotal);
if($conn->error){
    die($conn->error);
}
$total = $rs->fetch_assoc()["T"];

$pageCount = ceil($total/$pageSize);
if($pageIndex>$pageCount){
    $pageIndex = $pageCount;
}
if($pageIndex<1){
    $pageIndex = 1;
}

$startRow = ($pageIndex-1)*$pageSize;

$rows = [];
$rowsql = "SELECT * FROM article $where limit $startRow,$pageSize";
$rs = $conn->query($rowsql);
if($conn->error){
    die($conn->error);
}
while($row = $rs->fetch_assoc()){
    $rows[] = $row;
}

/*function makePageUrl($i){
    $querystring = "";
    foreach($_GET as $key=>$value){
        if($key==="pageIndex"){
            continue;
        }
        $querystring.="&".urlencode($key)."=".urlencode($value);
    }
    return $querystring;
}*/
function makePageUrl($pageIndex){
    $querystring = "pageIndex=$pageIndex";
    foreach($_GET as $k=>$v){
        if($k==="pageIndex"){
            continue;
        }
        $querystring.="&$k=$v";
    }
    return $querystring;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html charset=utf-8"/>
        <title>twelfth page</title>
    </head>
    <body>
        
        <style type="text/css">
        *{
            font-size: 14px;
            padding: 0;
            margin: 0;
        }
        table{
            border-collapse: collapse;
            background-color: #FFF;
            border: 1px solid #999;
            width: 100%;
        }
        table td,table th{
            padding: 6px;
            border-collapse: collapse;
            border:1px solid #333;
        }
        table th{
            background-color: #000;
            color:#999;
        }
        table th div.title{
            width:130px;
        }
        .filter{
            margin: 10px 0px;
            color: #666;
        }
        </style>

        <h3>文章列表</h3>
        <a href="test_7_article_add.php">新建文章</a>
        <input type="text" name="Title" id="keyword" value=""/>

        <script type="text/javascript">
        document.getElementById("keyword").onblur = function(){
            var key = document.getElementById("keyword").value;
            searchkey(key);
        }
        function searchkey(key,node){
            if(!node)node = document.body;
            for(let i=0,j=node,childNodes,length;i<j;i++){
                let child = node.childNodes[i];
                let val = child.nodeValue;
                if(!val){
                    if(child.childNodes && child.childNodes.length){
                        searchkey(key,child);
                    }else{
                        continue;
                    }
                }
                else{
                    var haskey = val.indexOF(key)>0;
                    if(haskey){
                        node.style.boder = "1px solid red";
                    }
                }
            }
        }
        </script>

        <form action="" class="filter" method="GET">
            <span>
                <label>标题</label>
                <input type="text" name="Title" id="Title" value="<?=$queryTitle?>"/>
            </span>
            <span>
                <label>作者</label>
                <input type="text" name="Author" value="<?=$queryAuthor?>" id="Title"/>
            </span>
            <span>
                <label>时间</label>
                <input type="text" name="minDate" value="<?=$queryMinDate?>"/>
                - <input type="text" name="maxDate" value="<?=$queryMaxDate?>"/>
            </span>
            <input type="submit" name="submit" value="搜索"/>
        </form>
        
        <script type="text/javascript">
        function jumpTo(pageIndex){
            var url = "?";
            var form = document.getElementById("form");
            var data = getDate(form,{})
            for(var n in data){
                url += "&"+ n + "=" + data[n];
            }
            url += "&pageIndex=" + pageIndex;
            location.href = url;
        }
        function getDate(form,data){
            for(let i=0,j=form.childNodes,length;i<j;i++){
                let child = form.childNodes[i];
                if(child.tagName=="INPUT"){
                    data[child.Name] = child.value;
                }
                if(child.childNodes && child.childNodes.length){
                    getDate(child,data);
                }
            }
            return data;
        }
        </script>

        <table border="1">
            <thead>
                <tr>
                    <th><div class="title">标题</div></th>
                    <th>作者</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($rows as $row){ ?>
                <tr>
                    <td>
                        <?=$row["Title"]?></a>
                    </td>
                    <td>
                        <?=$row["AuthorName"]?>
                    </td>
                    <td>
                        <?=$row["UpdateTime"]?> <br/>
                        <?=$row["CreateTime"]?> 
                    </td>
                    <td>
                        详细
                        <?php if($currentUser["Id"]===$row["AuthorID"]) {?>
                        <a href="test_7_art_modify.php?Id=<?=$row["Id"]?>">修改</a>
                        删除
                        <?php }?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4">
                        共<?=$total ?>条记录，<?=$pageCount ?>页，当前为<input type="text" value="<?=$pageIndex ?>" onblur="jumpTo(this.value)"/>页，
                        <a href = "test_7_art_list.php?<?=makePageUrl(1)?>">首页</a> <
                            <?php for($i = 1;$i<=$pageCount;$i++){ ?>
                                <a href="test_7_art_list.php?<?=makePageUrl($i)?>"><?=$i ?></a>
                            <?php }?>
                        > 
                        <a href = "test_7_art_list.php?<?=makePageUrl($pageCount)?>">尾页</a>
                    </td>
                </tr>
            </tfoot>
        </table>
    </body>
</html>